<?php

namespace Trawind\Events\Subscribers\Product\Sample;

use Trawind\Basics\Enums\BusinessType;
use Trawind\Events\Events\Inventory\Receipt\StoreReceiptEvent;
use Trawind\Events\Events\Inventory\Receipt\DeleteReceiptEvent;
use Trawind\Events\Events\Inventory\Receipt\StoreReceiptNoticeEvent;
use Trawind\Events\Events\Inventory\Receipt\DeleteReceiptNoticeEvent;
use Trawind\Remotes\Repositories\Product\Sample\SkuRepositoryEloquent;

class SkuSubscriber
{

    //入库
    const WAREHOUSING_ADD = 'warehousing_add';
    const WAREHOUSING_DEL = 'warehousing_del';


    //入库通知
    const WAREHOUSING_NOTIFICATION_ADD = 'warehousing_notification_add';
    const WAREHOUSING_NOTIFICATION_DEL = 'warehousing_notification_del';




    public function subscribe($events)
    {
        $events->listen(
            StoreReceiptNoticeEvent::class,
            function (StoreReceiptNoticeEvent $event) {
                if ($event->payload['business_type_id'] === BusinessType::PRODUCT_SAMPLE) {
                    app(SkuRepositoryEloquent::class)
                        ->callback($event->payload, static::WAREHOUSING_NOTIFICATION_ADD);
                }
            }
        );

        $events->listen(
            DeleteReceiptNoticeEvent::class,
            function (DeleteReceiptNoticeEvent $event) {
                if ($event->payload['business_type_id'] === BusinessType::PRODUCT_SAMPLE) {
                    app(SkuRepositoryEloquent::class)
                        ->callback($event->payload, static::WAREHOUSING_NOTIFICATION_DEL);
                }
            }
        );

        $events->listen(
            StoreReceiptEvent::class,
            function (StoreReceiptEvent $event) {
                if ($event->payload['business_type_id'] === BusinessType::PRODUCT_SAMPLE) {
                    app(SkuRepositoryEloquent::class)
                        ->callback($event->payload, static::WAREHOUSING_ADD);
                }
            }
        );

        $events->listen(
            DeleteReceiptEvent::class,
            function (DeleteReceiptEvent $event) {
                if ($event->payload['business_type_id'] === BusinessType::PRODUCT_SAMPLE) {
                    app(SkuRepositoryEloquent::class)
                        ->callback($event->payload, static::WAREHOUSING_DEL);
                }
            }
        );
    }

}